package scripts;

import javax.swing.JOptionPane;
import bot.ScriptMethods;
import javax.swing.*;
import java.awt.*;

public class MarketThieveAndBank extends ScriptMethods implements Runnable {

	public Thread scriptThread;
	public boolean scriptRunning = false;
	private int foodID;
	private int npcID;

	public void start() {
		System.gc();
		getAccountSettings();

		Object[] options2 = {"Guards", "Paladins", "Heroes"};
		String res2 = (String)JOptionPane.showInputDialog(null, "", "Select NPC to thieve", JOptionPane.PLAIN_MESSAGE, null, options2, options2[1]);


		Object[] options = {"Lobsters", "Swordfish", "Sharks"};
		String res1 = (String)JOptionPane.showInputDialog(null, "", "Select food to use", JOptionPane.PLAIN_MESSAGE, null, options, options[2]);

		if (res1.equals("Lobsters")) {
			foodID = 373;
		}

		if (res1.equals("Swordfish")) {
			foodID = 370;
		}

		if (res1.equals("Sharks")) {
			foodID = 546;
		}

		if (res2.equals("Guards")) {
			npcID = 321;
		}

		if (res2.equals("Paladins")) {
			npcID = 323;
		}

		if (res2.equals("Heroes")) {
			npcID = 324;
		}

		debug("Thieving " + res2 + " in Ardounge Market, eating " + res1 + ".");

		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		scriptRunning = false;
		System.gc();
		debug("Stopped script.");
	}

	public void run() {
		
		int[] startExp = setStartExp();
		long start = System.currentTimeMillis();
	    
		/*
		 * UI START
		 */
		JFrame frame = new JFrame("EXP change(NEW COMPILE)");
		frame.setSize(500, 500);
		frame.setVisible(true);
		JButton jButton1 = new JButton("Screenshot");
		frame.setLayout(null);		
		JLabel timeRunning = new JLabel("Running for: " );
		//JLabel attLabel = new JLabel("Attack Exp Gained: ");
		JLabel defLabel = new JLabel("Defense Exp Gained: ");
		JLabel strLabel = new JLabel("Thieving Exp Gained: ");
		//JLabel hitLabel = new JLabel("Hits Exp Gained: ");
		timeRunning.setBounds(15, 70, 300, 15);
		//attLabel.setBounds(15, 10, 200, 15);
		defLabel.setBounds(15, 10, 200, 15);
		strLabel.setBounds(15, 40, 200, 15);
		//hitLabel.setBounds(15, 100, 200, 15);
		frame.add(timeRunning);
		//frame.add(attLabel);
		frame.add(defLabel);
		frame.add(strLabel);
		//frame.add(hitLabel);
		/*
		 * UI END
		 */
	    
	    
		
		
		
		while (scriptRunning) {
			try {
				handleEvents();
				
				long elapsedTimeMillis = (System.currentTimeMillis()-start);
		        String timeRunningMillis = String.format("Running For: %d hour(s), %d minute(s), and %d second(s)", elapsedTimeMillis/(1000*60*60), (elapsedTimeMillis%(1000*60*60))/(1000*60), ((elapsedTimeMillis%(1000*60*60))%(1000*60))/1000);
				//if((elapsedTimeMillis / 1000f) >= 5){
					//attLabel.setText("Attack Exp Gained: " + expChange("Attack", startExp));
					defLabel.setText("Defense Exp Gained: " + expChange("Defense", startExp));
					strLabel.setText("Thieving Exp Gained: " + expChange("Thieving", startExp));
					//hitLabel.setText("Hits Exp Gained: " + expChange("Hits", startExp));
					timeRunning.setText(timeRunningMillis);
					
					
				if (bankOpen()) {
					if (invContains(619)) {
						depositAll(619);
						wait(random(600, 800));
					}
					if (invContains(38)) {
						depositAll(38);
						wait(random(600, 800));
					}
					if (invContains(152)) {
						depositAll(152);
						wait(random(600, 800));
					}
					if (invContains(142)) {
						depositAll(142);
						wait(random(600, 800));
					}
					if (invContains(161)) {
						depositAll(161);
						wait(random(600, 800));
					}
					if (invContains(612)) {
						depositAll(612);
						wait(random(600, 800));
					}
					if (invContains(10)) {
						depositAll(10);
						wait(random(600, 800));
					}
					if (invContains(41)) {
						depositAll(41);
						wait(random(600, 800));
					}
					withdraw(foodID, 15);
					wait(600);
					closeBank();
					wait(600);
				}
				if (inCombat()) {
					walkTo(ourPlayerX(), ourPlayerY(), false);
					wait(600);
				}
				if (!inCombat() && getCurrentLevel()[3] <= 30) {
					useItem(foodID);
					wait(600);
				}
				if (ourPlayerY() >= 609) {
					if (invContains(foodID)) {
						walkTo(548, 594, false);
						wait(2000);
					} else {
						if (!bankOpen()) {
							openBank();
							wait(2000);
						}
					}
				} else {
					if (invContains(foodID)) {
						int[] npcArray = getNpcIndex(new int[] {npcID});
						if(npcArray[0] != -1){
						  if (!isSleeping() && !inCombat() && getCurrentLevel()[3] > 30) {
							  thieveNpc(new int[] { npcID });
							  wait(random(600, 800));
						  }
						} else {
							walkTo(556, 595, false);
							wait(random(600, 800));
						}
					} else {
						walkTo(551, 612, false);
						wait(2000);
					}
				}
				wait(random(50, 80));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 80 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}
	
	public int expChange(String skill, int[] startExp){
		int[] currentExp = new int[18];
		int[] expDifference = new int[18];
		int val = -1;
		String[] statArray = {"Attack", "Defense", "Strength", "Hits", "Ranged", "Prayer", "Magic", "Cooking", "Woodcut", "Fletching", "Fishing", "Firemaking", "Crafting", "Smithing", "Mining", "Herblaw", "Agility", "Thieving"};
		

		for(int i = 0; i < 18; i++){
			if(statArray[i] == skill){
				return ((getCurrentExperience()[i] - startExp[i]) / 4);
			}
		}
		debug("You need to reference a skill by name!");
		debug("Right: Defense");
		debug("Wrong: defenSe, or, [1]");
		return -1;
		}
	
	public int[] setStartExp(){
		int[] startExp = new int[18];
		for(int i = 0; i < 18; i++){
			  startExp[i] = getCurrentExperience()[i];
			  wait(50);
		  }
		return startExp;
		
	}
}